/**
 * Created by Administrator on 2017/5/24 0024.
 */
$(function(){
    //6开关控制 能否注册
    var flag1 = false;
    var flag2 = false;
    var flag3 = false;
    var flag4 = false;
    var flag5 = false;
    var flag6 = false;
    //手机号验证
        $("#userTel").on({
            focus:function(){
                $(".yanzheng").css("display","block");
                $(".yanzheng1").text("11位数字,13-18开头");
            },
            blur:function(){
                var reg = /^[1][3-8]\d{9}$/;
                var yourTel = $(this).val();
                if(reg.test(yourTel)==true) {
                    $(".yanzheng").css("display","block");

                    $(".yanzheng1").text("格式正确")
                    flag1=true;
                }else{
                    $(".yanzheng").css("display","block");

                    $(".yanzheng1").text("格式错误")
                }
        }
        })
    //密码验证
    $("#userPwd").on({
        focus:function(){
            $(".yanzheng").css("display","block");
            $(".yanzheng2").text("最少6位，不能为中文");
        },
        blur:function(){
           var reg = /^\w{6,}$/;
           var yourPwd = $(this).val();
           if(reg.test(yourPwd)==true) {
               $(".yanzheng").css("display","block");

               $(".yanzheng2").text("格式正确");
               flag2=true;
           }else{
               $(".yanzheng").css("display","block");

               $(".yanzheng2").text("格式错误")
           }
       }
    })
    //常用邮箱验证
    $("#userEmail").on({
        focus:function(){
            $(".yanzheng").css("display","block");
            $(".yanzheng3").text("请用正确的邮箱格式");
        },
        blur:function(){
            var reg =  /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;;
            var yourEmail = $(this).val();
            if(reg.test(yourEmail)==true) {
                $(".yanzheng").css("display","block");

                $(".yanzheng3").text("格式正确");
                flag3=true;
            }else{
                $(".yanzheng").css("display","block");

                $(".yanzheng3").text("格式错误")
            }
        }
    })
    //常用用户名
    $("#userName").on({
        focus:function(){
            $(".yanzheng").css("display","block");
            $(".yanzheng4").text("不能少于2位");
        },
        blur:function(){
            var reg =  /^.{2,}$/;
            var yourEmail = $(this).val();
            if(reg.test(yourEmail)==true) {
                $(".yanzheng").css("display","block");

                $(".yanzheng4").text("格式正确");
                flag4=true;
            }else{
                $(".yanzheng").css("display","block");

                $(".yanzheng4").text("格式错误")
            }
        }
    })
    //验证功能 测试 不点击的时候
    $("#get_info").on("blur",function(){
        if($("#get_info").val().replace(/\s/g,"") ==  $("#auth_code").text().replace(/\s/g,"")){
            $(".yanzheng").css("display","block");

            $(".yanzheng5").text("验证码正确");
            flag5 = true;
        }else{
            $(".yanzheng").css("display","block");

            $(".yanzheng5").text("验证码错误");
        }
    })
    // 验证功能 测试 点击的时候
    $("#btn_get").on("click",function(){
        // 48 57 shuzi
        // 65 90 daxie zimu
        // 97 122 xiaoxe zimu
        //随机生成四位随机数
        function random(ranNum){
            if(ranNum>=97){
                return String.fromCharCode(ranNum);
            }else if(ranNum>=65 && ranNum <=90){
                return String.fromCharCode(ranNum);
            } else if(ranNum>=48 && ranNum<=57){
                return String.fromCharCode(ranNum);
            }else{
                return random(parseInt(Math.random()*123));
            }
        }
        var ranNum1 =   parseInt(Math.random()*123);
        var v1 = random(ranNum1);
        var ranNum2 =   parseInt(Math.random()*123);
        var v2 = random(ranNum2);
        var ranNum3 =   parseInt(Math.random()*123);
        var v3 = random(ranNum3);
        var ranNum4 =   parseInt(Math.random()*123);
        var v4 = random(ranNum4);
        var authCode = v1+" "+v2+" "+v3+" "+v4;
        $("#auth_code").text(authCode);
        //验证功能 测试
        $("#get_info").on("blur",function(){
            if($("#get_info").val().replace(/\s/g,"") ==  $("#auth_code").text().replace(/\s/g,"")){
                $(".yanzheng").css("display","block");
                $(".yanzheng5").text("");
                $(".yanzheng5").text("验证码正确");
                flag5 = true;
            }else{
                $(".yanzheng").css("display","block");
                $(".yanzheng5").text("");
                $(".yanzheng5").text("验证码错误");
            }
        })
    })
    //read 协议
    $("#cbx").on("click",function(){
        if($("#cbx").prop("checked") == true){
            flag6=true;
        }
    })
    //向服务器传输数据
    $("#btn_reg").on("click",function(){
        console.log(flag1,flag2,flag3,flag4,flag5,flag6);
        if(flag1==true && flag2==true && flag3==true && flag4==true && flag5==true && flag6==true) {
            $.ajax({
                url: "./../php/reg.php",
                type: "post",
                data: $("#myForm").serialize(),
                success: function (result) {

                    var obj = JSON.parse(result);
                    console.log(obj);
                    if (obj.status == 1) {
                        alert("注册成功");
                        window.location.href="../reg_login/login.html";
                    } else {
                        alert("注册失败");
                    }
                }
            })
        }else{
            alert("请补全信息,所有信息为必填选项，谢谢！")
        }
    })

})